<script setup>
import { User, Crop, EditPen, SwitchButton, CaretBottom } from '@element-plus/icons-vue'
import useUserStore from '@/stores/modules/user'
import router from '@/router'
import { ref } from 'vue'

// 导入仓库
const useuser = useUserStore()
// 获取用户信息
useuser.getUserInfo()
const isDialog = ref(false)
// 监听下拉菜单 如果点击的不是退出登录 那就跳转到对应的路径
const handleCommand = (command) => {
  if (command === 'logout') {
    isDialog.value = true
  } else {
    router.push(`/user/${command}`)
    console.log(command)
  }
}

// 判断用户是否要退出登录
const handelDialog = (key) => {
  if (key) {
    useuser.removeToken()
    useuser.user = {}
    router.push('/login')
    isDialog.value = false
  } else {
    isDialog.value = false
  }
}
</script>
<template>
  <div>
    <strong>{{ useuser.user.nickname }}</strong>
  </div>
  <el-dropdown placement="bottom-end" @command="handleCommand">
    <span class="el-dropdown__box">
      <el-avatar :src="useuser.user.user_pic" />
      <el-icon><CaretBottom /></el-icon>
    </span>
    <template #dropdown>
      <el-dropdown-menu>
        <el-dropdown-item command="profile" :icon="User">基本资料</el-dropdown-item>
        <el-dropdown-item command="avatar" :icon="Crop">更换头像</el-dropdown-item>
        <el-dropdown-item command="password" :icon="EditPen">重置密码</el-dropdown-item>
        <el-dropdown-item command="logout" :icon="SwitchButton">退出登录</el-dropdown-item>
      </el-dropdown-menu>
    </template>
  </el-dropdown>
  <el-dialog v-model="isDialog" title="温馨提示" width="500">
    <span>你确定要退出吗</span>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handelDialog(false)">取消</el-button>
        <el-button type="primary" @click="handelDialog(true)"> 确定 </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped>
.layout-container {
  height: 100vh;
  .el-aside {
    background-color: #232323;
    &__logo {
      height: 120px;
      background: url('@/assets/logo.png') no-repeat center / 120px auto;
    }
    .el-menu {
      border-right: none;
    }
  }
  .el-header {
    background-color: #fff;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .el-dropdown__box {
      display: flex;
      align-items: center;
      .el-icon {
        color: #999;
        margin-left: 10px;
      }

      &:active,
      &:focus {
        outline: none;
      }
    }
  }
  .el-footer {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    color: #666;
  }
}
</style>
